//
//  IGMyPaintingListManagerVC.m
//  jingdongfang
//
//  Created by boectoswteam on 2018/5/15.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//我的-我的画单管理
/*
 遗留问题
 1.几种跳转途径，我喜爱的画单 & 我创建的画单，还有其他的跳转路径吗？？？而且标题还有不一样的设置吗？
 2.什么是默认画单？从我喜爱的画单，跳转进入时，包含默认画单吗？
 3.画单列表应该是本界面获取 还是上个界面带过来的，而且画单列表接口还有分页功能，此时是否要加分页功能
 4.返回时，我是否要提供一个最新列表的数组
 */
#import "IGMyPaintingListManagerVC.h"
#import "IGMyPaintingListManagerCell.h"
#import "IGMyPaintingListManagerApi.h" //删除画单接口
#import "IGMyDrawingListApi.h" //获取我喜爱的 & 我创建的 画单列表接口

@interface IGMyPaintingListManagerVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) NSMutableArray *selectedArray;//选择的画单数据源
@property (nonatomic, strong) IGMyPaintingListManagerApi *api;//删除画单API
@property (nonatomic, assign) PAINTINGLIST_TYPE type;
@property (nonatomic, strong) IGMyDrawingListApi *drawingListApi;//画单列表接口
@property (nonatomic, strong) NSMutableArray *dataArray; // 画单列表数据源
@property (nonatomic, assign) NSInteger pageNums; //分页数
@end

static int pageNums;

@implementation IGMyPaintingListManagerVC

-(instancetype)initWithType:(PAINTINGLIST_TYPE)type{
    self = [super init];
    if (self) {
        self.type = type;
    }
    return self;
}

#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    pageNums = 1;
    self.title = NSLocalizedString(@"我的画单管理", nil);
    [self creatTableV];
    [self.view addSubview:self.deleteBtn];
    [self loadDrawingListData];
}

-(void)creatTableV{
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 44) style:UITableViewStylePlain];
    _tableV.delegate = self;
    _tableV.dataSource = self;
    [_tableV registerNib:[UINib nibWithNibName:@"IGMyPaintingListManagerCell" bundle:nil] forCellReuseIdentifier:@"IGMyPaintingListManagerCell"];
    _tableV.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableV];
}

-(void)loadDrawingListData{
    if (self.type == PAINTINGLIST_MY_TYPE) {
        self.drawingListApi = [[IGMyDrawingListApi alloc]initWithType: DRAWINGLIST_MYCREATE_TYPE];
    } else self.drawingListApi = [[IGMyDrawingListApi alloc]initWithType: DRAWINGLIST_MYFAVORITE_TYPE];
    self.drawingListApi.pageNum = [NSString stringWithFormat:@"%ld",(long)pageNums];
    self.drawingListApi.pageSize = @"10";
    __weak typeof(self) weakSelf = self;
    [self.drawingListApi startWithDecodeClass:[IGMyDrawingListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        IGMyDrawingListResp *response = responseObject;
        IGMyDrawingModel *model = response.data;
        NSArray *tempArray = [NSArray arrayWithArray:model.drawlist];
        if (tempArray.count!=0) {
            [weakSelf.dataArray addObjectsFromArray:[NSArray arrayWithArray:model.drawlist]];
            [weakSelf.tableV reloadData];
            pageNums ++;
            [weakSelf setupFooter];
        } else {
            //alert提示 没有更多内容了
            [weakSelf.tableV ig_endFooterRefreshingWithNoMoreData];
        }
    }];
}


- (void)setupFooter
{
    @KWeakObj(self)
    [_tableV ig_setFooterWithRefreshingBlock:^{
        [selfWeak loadDrawingListData];
    }];
}

#pragma mark - UITableViewDelegate && UITableViewDateSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IGMyPaintingListManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IGMyPaintingListManagerCell"];
    cell.data = [self.dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    IGMyPaintingListManagerCell *cell = (IGMyPaintingListManagerCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.isSelected = !cell.isSelected;
    IGDrawModel *model = self.dataArray[indexPath.row];
    if (cell.isSelected) {
        [self.selectedArray addObject:model.drawId];
        [cell.selectImageV setImage:[UIImage imageNamed:@"pushiGallery_Selected"]];
        NSLog(@"选中了%ld",indexPath.row);
    } else {
        [self.selectedArray removeObject:model.drawId];
        [cell.selectImageV setImage:[UIImage imageNamed:@"pushiGallery_Normal"]];
        NSLog(@"未选中%ld",indexPath.row);
    }
    [tableView reloadData];
}

#pragma mark - events

-(void)deletePictureList:(UIButton *)btn{
    
    if (self.selectedArray.count == 0) {
//        AlertView  请选择后删除
        [self presentMessageTips:NSLocalizedString(@"请先选择画单", nil)];
        return;
    }
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Notice", nil) message:NSLocalizedString(@"是否删除所选画单", nil) preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"是", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        NSString *pictureList = [self.selectedArray componentsJoinedByString:@","];
        __weak typeof(self) weakSelf = self;
        self.api = [[IGMyPaintingListManagerApi alloc]init];
        self.api.drawlists = pictureList;
        [self.api startWithDecodeClass:[IGMyPaintingListManagerResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            IGMyPaintingListManagerResp *response = responseObject;
            IGBaseRespHead *head = response.resHeader;
            if ([head.code isEqualToString:@"succcess"]) {
                pageNums = 1;
                [weakSelf loadDrawingListData];
            }
        }];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    [alert addAction:cancelAction];
    
    [self presentViewController:alert animated:YES completion:nil];

    
}

#pragma mark - set & get
-(UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.frame = CGRectMake(0, KSCREENHEIGHT - 60, KSCREENWIDTH, 60);
        _deleteBtn.backgroundColor = [UIColor c1Color];
        [_deleteBtn setImage:[UIImage imageNamed:@"picture_delete"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deletePictureList:) forControlEvents:UIControlEventTouchUpInside];
        [_deleteBtn setTitle:NSLocalizedString(@"删除", nil) forState:UIControlStateNormal];
        [_deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _deleteBtn.titleLabel.font = [UIFont font14];
        CGSize titleSize = CGSizeMake(27, 13);
        CGSize imageSize = _deleteBtn.imageView.bounds.size;
        CGFloat interval = 3;
        [_deleteBtn setImageEdgeInsets:UIEdgeInsetsMake(0,0,titleSize.height + interval, -titleSize.width)];
        [_deleteBtn setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height + interval, -imageSize.width, 0, 0)];
    }
    return _deleteBtn;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
